package network;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.FlowLayout;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EchoClient extends JFrame {
	
	BufferedWriter bw = null;

	private JPanel contentPane;
	private JPanel panel;
	private JButton btnJoin;
	private JButton btnOut;
	private JLabel lblNewLabel;
	private JTextField svIp;
	private JLabel lblNewLabel_1;
	private JTextField svPort;
	private JScrollPane scrollPane;
	private JTextPane result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EchoClient frame = new EchoClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EchoClient() {
		setTitle("Echo Client");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 725, 522);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getScrollPane(), BorderLayout.CENTER);
	}

	public void send(String a) throws Exception{
		bw.write(a);
	}
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			panel.add(getLblNewLabel());
			panel.add(getSvIp());
			panel.add(getLblNewLabel_1());
			panel.add(getSvPort());
			panel.add(getBtnJoin());
			panel.add(getBtnOut());
		}
		return panel;
	}
	private JButton getBtnJoin() {
		if (btnJoin == null) {
			btnJoin = new JButton("Join");
			btnJoin.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					btnJoin.setEnabled(false);
					btnOut.setEnabled(true);
					
					String ip = svIp.getText();
					int p = Integer.parseInt(svPort.getText());
					try{
						Socket s = new Socket(ip,p);
						OutputStream os = s.getOutputStream();
						OutputStreamWriter osw = new OutputStreamWriter(os);
						bw = new BufferedWriter(osw);
						for(int i=0; i<50; i++){ //bw.write(String a); 
							send("img src=http://cafeptthumb3.phinf.naver.net/20130822_137/wjdtmdghks5_1377165937411XEJki_JPEG/-227440918.jpg");	
						}
						
						
						bw.close();
						osw.close();
						os.close();
					}catch(Exception ex){
						ex.printStackTrace();
					}					
				}
			});
		}
		return btnJoin;
	}
	private JButton getBtnOut() {
		if (btnOut == null) {
			btnOut = new JButton("Out");
			btnOut.setEnabled(false);
			btnOut.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					btnJoin.setEnabled(true);
					btnOut.setEnabled(false);
				}
			});
		}
		return btnOut;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Server Address");
		}
		return lblNewLabel;
	}
	private JTextField getSvIp() {
		if (svIp == null) {
			svIp = new JTextField();
			svIp.setColumns(10);
			try{
				InetAddress ia = InetAddress.getLocalHost();
				svIp.setText(ia.getHostAddress());
			}catch(Exception ei){
				ei.printStackTrace();
			}
		}
		return svIp;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("Server Port");
		}
		return lblNewLabel_1;
	}
	private JTextField getSvPort() {
		if (svPort == null) {
			svPort = new JTextField();
			svPort.setText("4444");
			svPort.setColumns(10);
		}
		return svPort;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setContentType("text/html");
		}
		return result;
	}
}
